package com.kotlin.demo.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Checkbox
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.kotlin.demo.bean.TranslateConfigBean
import com.kotlin.demo.constant.Constants
import com.kotlin.demo.helper.DialogHelper
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed

@Composable
fun EditLanguageConfigDialog() {
    var languageList by remember { mutableStateOf(Constants.TRANSLATE_LIST_TAG.toMutableList()) }
    var newLanguage by remember { mutableStateOf(TranslateConfigBean("","","","","",false)) }

    Column(modifier = Modifier.padding(16.dp)) {
        // 替换显示多语言列表部分
        LazyColumn {
            itemsIndexed(languageList) { index, language ->
                Row(verticalAlignment = Alignment.CenterVertically) {
                    OutlinedTextField(
                        value = language.name,
                        onValueChange = { languageList[index] = language.copy(name = it) },
                        label = { Text("名称") },
                        modifier = Modifier.weight(1f)
                    )
                    OutlinedTextField(
                        value = language.shortName,
                        onValueChange = { languageList[index] = language.copy(shortName = it) },
                        label = { Text("简称") },
                        modifier = Modifier.weight(1f)
                    )
                    OutlinedTextField(
                        value = language.valuesName,
                        onValueChange = { languageList[index] = language.copy(valuesName = it) },
                        label = { Text("值名称") },
                        modifier = Modifier.weight(1f)
                    )
                    OutlinedTextField(
                        value = language.apiName,
                        onValueChange = { languageList[index] = language.copy(apiName = it) },
                        label = { Text("API名称") },
                        modifier = Modifier.weight(1f)
                    )
                    OutlinedTextField(
                        value = language.errorName,
                        onValueChange = { languageList[index] = language.copy(errorName = it) },
                        label = { Text("错误名称") },
                        modifier = Modifier.weight(1f)
                    )
                    Checkbox(
                        checked = language.isNeedTranslate,
                        onCheckedChange = { languageList[index] = language.copy(isNeedTranslate = it) },
                        modifier = Modifier.padding(start = 8.dp)
                    )
                }
            }
        }

        // 新增多语言输入框
        Row(verticalAlignment = Alignment.CenterVertically) {
            OutlinedTextField(
                value = newLanguage.name,
                onValueChange = { newLanguage = newLanguage.copy(name = it) },
                label = { Text("名称") },
                modifier = Modifier.weight(1f)
            )
            OutlinedTextField(
                value = newLanguage.shortName,
                onValueChange = { newLanguage = newLanguage.copy(shortName = it) },
                label = { Text("简称") },
                modifier = Modifier.weight(1f)
            )
            OutlinedTextField(
                value = newLanguage.valuesName,
                onValueChange = { newLanguage = newLanguage.copy(valuesName = it) },
                label = { Text("值名称") },
                modifier = Modifier.weight(1f)
            )
            OutlinedTextField(
                value = newLanguage.apiName,
                onValueChange = { newLanguage = newLanguage.copy(apiName = it) },
                label = { Text("API名称") },
                modifier = Modifier.weight(1f)
            )
            OutlinedTextField(
                value = newLanguage.errorName,
                onValueChange = { newLanguage = newLanguage.copy(errorName = it) },
                label = { Text("错误名称") },
                modifier = Modifier.weight(1f)
            )
            Checkbox(
                checked = newLanguage.isNeedTranslate,
                onCheckedChange = { newLanguage = newLanguage.copy(isNeedTranslate = it) },
                modifier = Modifier.padding(start = 8.dp)
            )
            Button(onClick = { languageList.add(newLanguage); }) {
                Text("新增")
            }
        }

        // 保存和取消按钮
        Row(Modifier.padding(top = 16.dp), horizontalArrangement = Arrangement.End) {
            Button(onClick = { DialogHelper.dismissDialog() }) {
                Text("保存")
            }
            Button(onClick = { DialogHelper.dismissDialog() }, modifier = Modifier.padding(start = 8.dp)) {
                Text("取消")
            }
        }
    }
}